package com.zaizai.service.impl;

import com.zaizai.domain.Role;
import com.zaizai.domain.dto.CommodityDto;
import com.zaizai.domain.dto.OrderCommodityDto;
import com.zaizai.service.IChartsService;
import com.zaizai.service.IOrderService;
import com.zaizai.service.IRoleService;
import com.zaizai.utils.BaseContext;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ChartsServiceImpl implements IChartsService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IRoleService roleService;

    //获取当前用户id
    private final Long userId = BaseContext.getCurrentId();
    //根据需求利用hash结构存储值
    //拿到哈希的操作对象
    private HashOperations<String, String, CommodityDto> Operations;

    // 延迟初始化 Operations
    @PostConstruct
    public void init() {
        Operations = redisTemplate.opsForHash();
    }

    @Override
    public List<CommodityDto> selectCommodities() {
        //从redis中查询用户id为userId的商品信息
        return Operations.values("Charts:" + userId);
    }

    @Override
    public void InsertCommodity(CommodityDto commodityDto) {
        //以用户id为主键，商品id为副主键存储整个商品信息
        Operations.put("Charts:" + userId, commodityDto.getId().toString(), commodityDto);
    }

    @Override
    public void deleteCommodities(Long[] ids) {
        for (Long id : ids) {
            //以用户id为主键，商品id为副主键删除商品信息
            Operations.delete("Charts:" + userId, id.toString());
        }
    }

    @Override
    public void deleteCommodity(Long id) {
        //以用户id为主键，商品id为副主键删除商品信息
        Operations.delete("Charts:" + userId, id.toString());
    }

    @Override
    public void updateCommodity(CommodityDto commodityDto) {
        //以用户id为主键，商品id为副主键更新商品信息
        Operations.put("Charts:" + userId, commodityDto.getId().toString(), commodityDto);
    }

    @Override
    @Transactional
    public void purchaseCharts() {
        log.info("当前用户Id:{}", userId);
        //获取当前用户信息
        Role role = roleService.selectRoleById(userId);
        log.info("当前用户信息:{}", role);
        //获取当前用户的购物车信息
        List<CommodityDto> commodityDtoList = this.selectCommodities();
        //新建OrderCommodityDto对象，用于封装订单信息
        OrderCommodityDto orderCommodityDto = new OrderCommodityDto();
        //把用户信息和商品信息封装到OrderCommodityDto对象中
        BeanUtils.copyProperties(role, orderCommodityDto);
        orderCommodityDto.setCommodityList(commodityDtoList);
        //调用订单服务进行下单
        if (!commodityDtoList.isEmpty()) {
            orderService.insertOrder(orderCommodityDto);
        }
        //买完后，清空用户id为userId的商品信息
        Operations.delete("Charts:" + userId);

        log.info("购物车下单成功:{}", orderCommodityDto);
    }
}
